package com.uchengwang.ui.cm_ui.uu.fm;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.ui.base_ui.basefm.BaseFragment;
import com.uchengwang.ui.cm_ui.rychat.manager.RyGroupManager;
import com.uchengwang.ui.cm_ui.uu.adapter.RyGroupInviteAdapter;
import com.uchengwang.ui.cm_ui.uu.bean.RyGroupInviteBean;

import java.util.List;

/**
 * Created by U城网 on 2017/10/22.
 * 别人对你的群申请信息
 */

public class GroupApplyFragment extends BaseFragment{
    private View view;
    private RecyclerView rv;
    private int page = 1;//当前页  每页请求多少数据
    private int totlaPagers ;//总页数【服务器返回】
    private RyGroupInviteAdapter adapter;
    private PtrClassicFrameLayout refresh;
    private RecyclerAdapterWithHF mAdapter;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        setLayoutId(R.layout.activity_share2);
        view = super.onCreateView(inflater, container, savedInstanceState);
        initView(view);// 控件初始化
        initData();

        return view;
    }


    private void initView(View view){
        refresh = (PtrClassicFrameLayout) view.findViewById(R.id.share2_refresh);
        rv = (RecyclerView) view.findViewById(R.id.share2_rv);

        initData();
    }


    private void initData(){
        rv.setLayoutManager(new LinearLayoutManager(this.getContext()));
        adapter = new RyGroupInviteAdapter(this.getContext(), 1);//1申请 2邀请
        mAdapter = new RecyclerAdapterWithHF(adapter);
        rv.setAdapter(mAdapter);

        refresh.postDelayed(new Runnable() {

            @Override
            public void run() {
                refresh.autoRefresh(true);
            }
        }, 150);

        refresh.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                getData();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    //获取群申请信息
    private void getData(){
        RyGroupManager.getManager().checkApply(this.getActivity(), new OnHttpResultListener<RyGroupInviteBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<RyGroupInviteBean> list) {
                if (refresh.isRefreshing()){
                    refresh.refreshComplete();
                }

                if (status ==1){
                    adapter.getData().clear();
                    adapter.notifyDataSetChanged();
                    return;
                }

                if (list != null && list.size() >0){
                    adapter.setData(list);
                }
            }
        });
    }


    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        List<RyGroupInviteBean> ucitys = adapter.getData();
        if (ucitys != null){
            ucitys.clear();
            ucitys = null;
        }

    }


}
